package com.waterdrop.wlsggk.service.impl;


import com.waterdrop.wlsggk.mapper.DishMapper;
import com.waterdrop.wlsggk.pojo.Dish;
import com.waterdrop.wlsggk.pojo.DishMaterial;
import com.waterdrop.wlsggk.pojo.dto.DishDto;
import com.waterdrop.wlsggk.service.DishMaterialService;
import com.waterdrop.wlsggk.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜品信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class DishServicelmpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishMaterialService dishMaterialService;

    /**
     * 查询菜品数据集合
     *
     * @param Dish 菜品信息
     * @return 菜品数据集合
     */
    @Override
    public List<Dish> selectDishList(Dish Dish) {
        return dishMapper.selectDishList(Dish);
    }


    /**
     * 根据菜品id查询信息
     *
     * @param id 菜品ID
     * @return 结果
     */
    @Override
    public Dish selectwlsggkDishById(Long id) {
        return dishMapper.selectwlsggkDishById(id);
    }

    /**
     * 添加菜品
     *
     * @param dish 菜品信息
     * @return 结果
     */
    @Override
    public int insertWlsggkDish(Dish dish) {
        return dishMapper.insertWlsggkDish(dish);
    }

    /**
     * 添加菜品，同时需要保存菜品的物料的关联关系
     *
     * @param dishDto
     * @return
     */
    @Override
    @Transactional
    public int insertDishWithMaterial(DishDto dishDto) {
        //添加菜品表
//        this.insertWlsggkDish(dishDto);
//
//        List<DishMaterial> dishMaterials = dishDto.getDishMaterials();
//
//        dishMaterials.stream().map((item) -> {
//            item.setWlsggkDishId(dishDto.getId());
//            return item;
//        }).collect(Collectors.toList());
//
//        //保存菜品和物料的关联信息
//        return dishMaterialService.insertWlsggkDishMaterial(dishMaterials);
        return 1;
    }

    /**
     * 删除菜品信息
     *
     * @param ids 菜品ID
     * @return 结果
     */
    @Override
    public int deleteWlsggkDish(Long[] ids) {
        return dishMapper.deleteWlsggkDishByIds(ids);
    }

    /**
     * 删除菜品信息，删除菜品物料关联表的信息
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public int deleteWlsggkDishWithMaterial(Long[] ids) {
        //删除菜品信息
        this.deleteWlsggkDish(ids);

        //删除菜品物料关联表的信息
        return dishMaterialService.deleteWlsggkDishMaterial(ids);
    }

    @Override
    @Transactional
    public DishDto selectwlsggkDishWithMaterialById(Long id) {
        //查询菜品表
        Dish dish = this.selectwlsggkDishById(id);

        DishDto dishDto = new DishDto();
        //对象拷贝
        BeanUtils.copyProperties(dish, dishDto);
        //通过菜品id获取菜品物料信息
        List<DishMaterial> dishMaterials = dishMaterialService.selectWlsggkDishMaterialByDishId(id);

//        dishDto.setDishMaterials(dishMaterials);

        return dishDto;
    }

    /**
     * 修改菜品
     *
     * @param dish 物料信息
     * @return 结果
     */

    @Override
    @Transactional
    public int updateWlsggkDish(Dish dish) {

        return dishMapper.updateWlsggkDish(dish);

/*        wlsggkDishMaterialService.deleteWlsggkDishMaterial(new Long[] { wlsggkDishDto.getId() });

        //获取wlsggkDishDto中的WlsggkDishMaterial集合
        List<WlsggkDishMaterial> wlsggkDishMaterials = wlsggkDishDto.getWlsggkDishMaterials();

        //将wlsggkDishDto中的主键id回填到wlsggkDishMaterials的dishId
        wlsggkDishMaterials.stream().map((item) -> {
            item.setDishId(wlsggkDishDto.getId());
            return item;
        }).collect(Collectors.toList());

        return wlsggkDishMaterialService.insertWlsggkDishMaterial(wlsggkDishMaterials);*/

    }
}

